<?php

declare(strict_types=1);

/**
 * @Author:     Frank
 * @contact     frank_hhm@163.com
 * @Date:       2022-11-15 16:04:01
 * @Last Modified time: 2022-11-27 10:22:30
 */
namespace app\common\services\system;

use frank\base\BaseService;
use frank\utils\Arr;
use app\common\dao\system\ConfigDao;
use frank\exception\CommonException;

/**
 * 系统配置项
 * Class ConfigService
 */
class ConfigService extends BaseService
{
	/**
     * ConfigService constructor.
     * @param ConfigDao $dao
     */
    public function __construct(ConfigDao $dao)
    {
        $this->dao = $dao;
    }

    public function getConfigDetail($filter)
    {
        $detail = $this->dao->detail($filter);
        if (!$detail) {
            throw new CommonException('数据不存在');
        }
        return $detail->toArray();
    }

    /**
     * 获取配置列表
     */
    public function getConfigList(array $where)
    {
        [$page, $limit] = $this->getPageValue();
        $list = $this->dao->getConfigList($where, $page, $limit);
        return $list;
    }

    public function getConfigItemsByTabId($tabId = 0){
        $list = $this->dao->getConfigSelect(['config_tab_id'=>$tabId]);
        $form = [];
        foreach ($list as $key => $item) {
            $form[$item['name_field']] = $item['value'] ?? '';
        }
        return ['form'=>$form,'rule'=>$list];
    }   
}